/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mahumLogs.utils;

import javax.faces.application.FacesMessage;

/**
 *
 * @author Kalast
 */
public class DataValidation {
    
    public static FacesMessage ip(String ip){
        FacesMessage msg = null;
        ip = ip.replace(".", ":");
        String[] partsIPString = ip.split(":");
        if(ip.isEmpty() || ip.length() > 15 || partsIPString.length != 4){
            System.err.println(ip + " is too length or to short " + (partsIPString.length));
            msg = new FacesMessage("Adresse IP invalide !", "ex : 127.0.0.1");
        }

        int test;
        for(String partS : partsIPString){
            try{
                test = Integer.valueOf(partS);
                partS = test+"";
                if(test < 0 || test > 255){
                    msg = new FacesMessage("Adresse IP invalide !", "ex : 127.0.0.1");
                    break;
                }
            } catch(Exception ex){
                System.err.println(ip + " contains letters");
                msg = new FacesMessage("Adresse IP invalide !", "ex : 127.0.0.1");
                break;
            }
        }
        
        return msg;
    }
    
    public static FacesMessage user(String user){
        System.out.println("Valid user");
        FacesMessage msg = null;
        if(user.isEmpty() || user.contains(" ") || user.length() < 3 || user.length() > 24){
            msg = new FacesMessage("Nom d'utilisateur invalide !", "ex : root");
        }
        return msg;
    }
    
    public static FacesMessage password(String pwd){
        System.out.println("Valid pwd");
        FacesMessage msg = null;
        if(pwd.isEmpty()){
            msg = new FacesMessage("Mot de passe invalide !", "Ne peut pas être vide !");
        }
        return msg;
    }
    
    public static FacesMessage path(String path){
        System.out.println("Valid path");
        FacesMessage msg = null;
        if(path.isEmpty()){
            msg = new FacesMessage("Nom d'utilisateur invalide !", "ex : root");
        }
        
        if(path.contains("/") && path.contains("\\") 
                || path.contains("\"") 
                || path.contains("*") 
                || path.contains("?") 
                || path.contains("<") 
                || path.contains(">") 
                || path.contains("|") 
                || path.contains(":")){
            msg = new FacesMessage("Path invalide !", "Ne peut pas contenir les caractères : \\ / * ? < > | : ");
        }
        
        return msg;
    }
}
